<template>
  <div id="join-allot" v-if="getAllotInfo">
    <top-bar title="加入行程" @goback="goback"></top-bar>
    <div class="join-info-container">
      <div class="column">
        <div class="row">
          <driver-avatar
            :url="getAllotInfo.driverImage==''?defaultDriverImage:getAllotInfo.driverImage"
            :show-text="superDriver"
            :text="getAllotInfo.driverSpan==''?'':getAllotInfo.driverSpan"
          ></driver-avatar>
          <!-- <img class="driver-head" :src="getAllotInfo.driverImage==''?defaultDriverImage:getAllotInfo.driverImage" /> -->
          <div class="column ml-2">
            <div class="row centered">
              <div style="font-size:28px">{{getAllotInfo.driverName==''?'未指派':getAllotInfo.driverName}}</div>
              <rate class="mx-2" v-if="getAllotInfo.driverLevel" v-model="driverNumber" readonly></rate>
              <div style="font-size:20px;color:#666666">{{getAllotInfo.driverLevel==''?'':getAllotInfo.driverLevel}}</div>
            </div>
            <div class="row centered">
              <div style="color:#666666;font-size:26px;line-height:36px">{{getAllotInfo.variety==''?'未知车型':getAllotInfo.variety}}</div>
              <div style="color:#666666;margin-left:10px:font-size:26px;line-height:36px">{{getAllotInfo.licensePlate==''?'':getAllotInfo.licensePlate}}</div>
            </div>
          </div>
        </div>
      </div>
      <div class="row between end mt-2">
        <div class="column">
          <div class="row centered">
            <md-icon class="icon" name="time"></md-icon>
            <div class="mx-2" style="color:#999999">接驾时间</div>
            <div>{{getAllotInfo.driverTime}}</div>
          </div>
          <div class="row centered my-1">
            <md-icon class="icon" name="time"></md-icon>
            <div class="mx-2" style="color:#999999">出发时间</div>
            <div>{{getAllotInfo.travelTime}}</div>
          </div>
          <div class="row centered">
            <md-icon class="icon" name="seats"></md-icon>
            <div class="mx-2" style="color:#999999">剩余座位</div>
            <div>{{getAllotInfo.seatFree}}位</div>
          </div>
        </div>
        <div class="row">
          <div style="font-size:26px;color:#666666">￥{{getAllotInfo.orderAmt}}起</div>
        </div>
      </div>
    </div>
    <div class="selector-container" v-if="getRoute&&getAllotInfo.status == '0'">
      <md-field>
        <md-input-item
          :value="startInfoString"
          class="no-right-padding"
          ref="startLocation"
          :placeholder="getRoute?getRoute.start_auto_name +'  从哪出发':''"
          readonly
          @click.native="selectStart"
        >
          <md-icon class="place_icon" name="shangchedian" slot="left"></md-icon>
        </md-input-item>
        <md-input-item
          :value="endInfoString"
          class="no-right-padding"
          ref="endLocation"
          :placeholder="getRoute?getRoute.end_auto_name + '  您要去哪':''"
          readonly
          @click.native="selectEnd"
        >
          <md-icon class="place_icon" name="xiachedian" slot="left"></md-icon>
          <div v-if="false" slot="right" class="out_tag">
            不在服务范围内
            <md-icon class="tk_close" name="tk_close"></md-icon>
          </div>
        </md-input-item>
        <md-field-item :customized="true" class="no-right-padding">
          <md-input-item
            class="inline_field"
            ref="numSelector"
            placeholder="乘车人数"
            readonly
            slot="left"
            :value="pinNumPassenger?pinNumPassenger!=0?pinNumPassenger+'人':'':''"
            @click.native="selectPassenegerNum"
          >
            <md-icon class="place_icon" name="pc_pcrs" slot="left"></md-icon>
          </md-input-item>
          <md-input-item
            class="inline_field"
            ref="remarks"
            placeholder="出行备注"
            readonly
            slot="right"
            :value="getPinSelectedRemarks?getPinSelectedRemarks.length>0?'已添加':'':''"
            @click.native="selectCommet"
          >
            <md-icon class="place_icon" name="chuxingbeizhu" slot="left"></md-icon>
          </md-input-item>
        </md-field-item>
        <div class="cost-info" v-show="getAllotCostInfo">
          <div class="row centered">
            <div class="price">
              <span class="symbol">￥</span>
              {{getAllotCostInfo?getAllotCostInfo.prefePrice:''}}
            </div>
            <md-icon name="wdqb-more" @click="gotoCostDetail"></md-icon>
          </div>
          <div class="desc" v-if="getAllotCostInfo&&getAllotCostInfo.cheapFlag=='1'">
            原价{{getAllotCostInfo.price}}元，卷已抵
            <span style="color:#FF6917">{{getAllotCostInfo.cheapPrice}}</span>元
          </div>
          <div class="gaomsg" v-show="isGaosuMsg">
            <img src="@/assets/imgs/tk_ts.png" />
            {{getAllotCostInfo?getAllotCostInfo.gaosuMsg:""}}
          </div>
        </div>
        <md-button class="submit-btn" @click="submitOrder" :disabled="!getAllotCostInfo">加入该行程</md-button>
      </md-field>
    </div>
    <div class="join-allot-download-app" v-show="showDownloadApp">
      <div class="row centered">
        <img :src="require('@/assets/imgs/logo_share.png')" />
        <div class="download-app-tip-text">下载平安到家，豪礼随你拿！</div>
      </div>
      <md-button class="download-app-btn" size="small" @click="gotoDownloadApp">去下载</md-button>
      <md-icon class="download-app-close-btn" name="close_icon" @click="closeDownloadApp"></md-icon>
    </div>
  </div>
</template>

<script>
import '@/assets/svgs/time.svg';
import '@/assets/svgs/seats.svg';
import '@/assets/svgs/shangchedian.svg';
import '@/assets/svgs/xiachedian.svg';
import '@/assets/svgs/pc_pcrs.svg';
import '@/assets/svgs/chuxingbeizhu.svg';
import '@/assets/svgs/wdqb-more.svg';
import '@/assets/svgs/close_icon.svg';
import { mapGetters } from 'vuex';
import TopBar from '@/components/TopBar';
import DriverAvatar from '@/components/DriverAvatar';
import {
  GET_ALLOT_INFO_FLAG,
  GET_ROUTE_INFO,
  GET_PIN_STATIONS_INFO,
  GET_ORDER_TIME,
  GET_REMARKS,
  GET_ALLOT_COST_INFO,
  ORDER_SUBMIT
} from '@/store/actions.types';
import { Dialog, Button, Icon, Field, FieldItem, InputItem, Tag } from 'mand-mobile';
import Popup from './popup';
import Map from '@/components/map';
import { decryptByDESModeEBC, encryptByDESModeEBC } from '@/common/encrypt';
import Utils from '@/common/pakjUtils';
import Rate from '@/components/Rate';
import XBack from 'xback';
var moment = require('moment');
moment.locale('zh-cn');
export default {
  name: 'join-allot',
  components: {
    [Field.name]: Field,
    [FieldItem.name]: FieldItem,
    [InputItem.name]: InputItem,
    [TopBar.name]: TopBar,
    [Button.name]: Button,
    [Rate.name]: Rate,
    [DriverAvatar.name]: DriverAvatar,
    [Icon.name]: Icon,
    Tag
  },
  data() {
    return {
      superDriver: false,
      showDownloadApp: true,
      xBack: null,
      isGaosuMsg: false
    };
  },
  created() {
    this.xBack = XBack.init(window, event => {
      this.xBack.back();
      this.xBack.uninit();
      this.$store.commit('resetRoute');
    });
    let allotid = this.$route.query.allotid;
    if (!allotid) {
      Dialog.alert({
        title: '温馨提示',
        content: '当前行程已经结束',
        onConfirm: () => {
          this.$router.replace({
            name: 'Home'
          });
        }
      });
      return;
    }
    this.$store
      .dispatch(GET_ALLOT_INFO_FLAG, allotid)
      .then(() => {
        this.getCostInfo();
        if (!this.getRoute) {
          this.$store
            .dispatch(GET_ROUTE_INFO, this.getAllotInfo.routeId)
            .then(() => {
              this.$store.dispatch(GET_PIN_STATIONS_INFO);
              // this.$store.dispatch(GET_ORDER_TIME);
              console.log(this.getAllotInfo);
              if (this.getAllotInfo.driverSpan) {
                this.superDriver = true;
              }
            })
            .catch(reason => {
              Dialog.alert({
                title: '错误',
                content: reason.message
              });
            });
        }
      })
      .catch(reason => {
        Dialog.alert({
          title: '错误',
          content: reason.message
        });
      });
  },
  methods: {
    goback() {
      this.$store.commit('resetRoute');
    },
    selectStart() {
      Map.createMap(true, '1', this.getRoute.pin_opt_type_start).then(() => {
        this.getCostInfo();
      });
    },
    selectEnd() {
      Map.createMap(false, '1', this.getRoute.pin_opt_type_end).then(() => {
        this.getCostInfo();
      });
    },
    selectPassenegerNum() {
      Popup.createNumberSelector(parseInt(this.getAllotInfo.seatFree)).then(() => {
        this.getCostInfo();
      });
    },
    selectCommet() {
      Popup.createRemarkSelector('1');
    },
    getCostInfo() {
      if (!this.canGetCostInfo) {
        return;
      }
      let params = {
        route_id: this.getRoute.route_id,
        allot_id: this.getAllotInfo.allotId,
        origin_coord: encryptByDESModeEBC(this.getPinStartInfo.coordinate[0] + ',' + this.getPinStartInfo.coordinate[1]),
        dest_coord: encryptByDESModeEBC(this.getPinEndInfo.coordinate[0] + ',' + this.getPinEndInfo.coordinate[1]),
        // travel_time: this.getAllotInfo.driverTime,
        // travel_end_time: this.getAllotInfo.travelTime,
        order_num: this.pinNumPassenger.toString()
      };
      if (this.getPinStartInfo.routeStationId) {
        params.start_site = this.getPinStartInfo.routeStationId;
      }
      if (this.getPinEndInfo.routeStationId) {
        params.end_site = this.getPinEndInfo.routeStationId;
      }
      this.$store.dispatch(GET_ALLOT_COST_INFO, params).then(() => {
        console.log(this.getAllotCostInfo);
        if (this.getAllotCostInfo.gaosuMsg) {
          this.isGaosuMsg = true;
        }
      });
    },
    submitOrder() {
      console.log(this.getAllotInfo);
      let params = {
        route_id: this.getRoute.route_id,
        allot_id: this.getAllotInfo.allotId,
        origin: this.getPinStartInfo.stationName,
        origin_label_detailed: this.getPinStartInfo.position,
        origin_coord: encryptByDESModeEBC(this.getPinStartInfo.coordinate[0] + ',' + this.getPinStartInfo.coordinate[1]),
        destination: this.getPinEndInfo.stationName,
        dest_label_detailed: this.getPinEndInfo.position,
        dest_coord: encryptByDESModeEBC(this.getPinEndInfo.coordinate[0] + ',' + this.getPinEndInfo.coordinate[1]),
        travel_time: moment(this.getAllotInfo.driverTime, 'YYYY-MM-DD HH:mm')
          .valueOf()
          .toString(),
        travel_end_time: moment(this.getAllotInfo.travelTime, 'YYYY-MM-DD HH:mm')
          .valueOf()
          .toString(),
        order_num: this.pinNumPassenger.toString(),
        remarks: this.getPinSelectedRemarksString,
        systemType: Utils.getCurrentEnv(),
        platformId: this.getPlatformId,
        order_type: '拼车'
      };
      if (this.getPinStartInfo.routeStationId) {
        params.start_site = this.getPinStartInfo.routeStationId;
      }
      if (this.getPinEndInfo.routeStationId) {
        params.end_site = this.getPinEndInfo.routeStationId;
      }
      this.$store
        .dispatch(ORDER_SUBMIT, params)
        .then(() => {
          this.$router.push({
            name: 'OrderPayment',
            query: {
              orderno: this.getOrderInfoData.order_no
            }
          });
          this.$store.commit('resetRoute');
        })
        .catch(error => {
          Dialog.alert({
            title: '温馨提示',
            content: error.message,
            confirmText: '确定',
            onConfirm: () => {
              this.$router.go(-1);
            }
          });
        });
    },
    gotoCostDetail() {
      this.$router.push({
        name: 'beforechargesDetails',
        query: {
          orderno: this.getAllotCostInfo.cost_id
        }
      });
    },
    gotoDownloadApp() {
      window.location.href = '//appurl.me/14117371';
    },
    closeDownloadApp() {
      this.showDownloadApp = false;
    }
  },
  computed: {
    ...mapGetters([
      'getRoute',
      'getPlatformId',
      'getAllotInfo',
      'getAllotCostInfo',
      'getPinStartInfo',
      'getPinEndInfo',
      'pinNumPassenger',
      'getPinSelectedRemarks',
      'getPinSelectedRemarksString',
      'getOrderInfoData'
    ]),
    canGetCostInfo() {
      return this.getPinStartInfo && this.getPinEndInfo && this.pinNumPassenger !== 0;
    },
    startInfoString() {
      let str = '';
      if (this.getPinStartInfo) {
        str = this.getRoute.start_auto_name + '  ' + this.getPinStartInfo.stationName;
      }
      return str;
    },
    driverNumber() {
      return parseFloat(this.getAllotInfo.driverLevel);
    },
    endInfoString() {
      let str = '';
      if (this.getPinEndInfo) {
        str = this.getRoute.start_auto_name + '  ' + this.getPinEndInfo.stationName;
      }
      return str;
    }
  }
};
</script>

<style lang="stylus">
#join-allot {
  background-color: color-primary-background;
  height: 100vh;
  font-size: 24px;

  .join-info-container {
    margin: 20px;
    padding: 30px 28px 26px 28px;
    background-color: #fff;
    border-radius: radius-normal;

    .driver-head {
      width: 80px;
      height: 80px;
    }

    .icon {
      width: 20px;
      height: 16px;
    }
  }

  .selector-container {
    margin: 20px;
    background-color: #fff;
    border-radius: radius-normal;
    padding: 10px;

    .md-field-item {
      .md-field-item-inner {
        padding-top: 0px;
        padding-bottom: 0px;
      }
    }

    .no-right-padding {
      padding-left: 20px !important;
      padding-right: 20px !important;
    }

    .place_icon {
      width: 20px;
      height: 20px;
    }

    .inline_field {
      padding: 0px;
    }

    .cost-info {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      padding: 20px;

      .price {
        width: 209px;
        font-size: 52px;
        text-align: center;

        .symbol {
          font-size: 36px;
        }
      }

      .desc {
        width: 283px;
        height: 34px;
        font-size: 24px;
        text-align: center;
        width: 100%;
        color: color-text-caption;
      }
    }

    .submit-btn {
      margin: 10px;
      width: auto !important;
    }
  }

  .join-allot-download-app {
    position: absolute;
    bottom: 0;
    width: 100vw;
    height: 100px;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0px 40px 0px 20px;
    box-sizing: border-box;

    img {
      width: 60px;
      height: 60px;
    }

    .download-app-tip-text {
      font-size: 28px;
      font-weight: 500;
      color: #fff;
      line-height: 40px;
      margin-left: 14px;
    }

    .download-app-btn {
      width: 144px;
      height: 64px;
      font-size: 32px;
    }

    .download-app-close-btn {
      position: absolute;
      right: 0;
      top: 0;
      width: 28px;
      height: 28px;
    }
  }

  .gaomsg {
    color: #ff6917;
  }

  .gaomsg img {
    width: 26px;
    height: 24px;
  }
}
</style>
